package com.wanhe.callring.module.callring;

import com.orhanobut.logger.Logger;
import com.wanhe.base.model.entity.CallRing;
import com.wanhe.base.net.ServerApi;
import com.wanhe.base.runner.Runner;
import com.wanhe.callring.module.base.IBasePresenter;
import com.wanhe.callring.module.base.ILoadDataView;

import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Consumer;

/**
 * 呼叫铃Presenter
 * Created by XieZaiWei on 2017/2/15.
 */

public class CallRingPresenter implements IBasePresenter<ILoadDataView> {

	private ILoadDataView mCallRingView;

	private ServerApi api;
	private Runner runner;

	public CallRingPresenter() {
		api = new ServerApi();
		runner = new Runner();
	}

	@Override
	public void setView(ILoadDataView view) {
		mCallRingView = view;
	}

	@Override
	public void callRing(String tableName, String message) {
		runner.execute(api.callRing(tableName, message), new Consumer<CallRing>() {
			@Override
			public void accept(@NonNull CallRing callRingResult) throws Exception {
				int resultType = callRingResult.getResultType();
				if (resultType == 1) {
					Logger.i("callRing", "=========" + callRingResult.getResultMsg());
					mCallRingView.loadData(callRingResult.getResultMsg());
				} else {
					mCallRingView.loadData("呼叫失败,请稍后重试!");
				}

			}
		});


//		RetrofitService.callRing(tableName, message)
//				.doOnSubscribe(new Consumer<Disposable>() {
//					@Override
//					public void accept(@NonNull Disposable disposable) {
//						mCallRingView.showLoading();
//					}
//				})
//				.compose(mCallRingView.<CallRing>bindToLife())
//				.subscribe(new Observer<CallRing>() {
//					@Override
//					public void onSubscribe(@NonNull Disposable d) {
//
//					}
//
//					@Override
//					public void onError(@NonNull Throwable e) {
//						Logger.e(e.toString());
////						mCallRingView.showNetError(new EmptyLayout.OnRetryListener() {
////							@Override
////							public void onRetry() {
////							}
////						});
//					}
//
//					@Override
//					public void onComplete() {
//						mCallRingView.hideLoading();
//					}
//
//					@Override
//					public void onNext(@NonNull CallRing callRingResult) {
//						int resultType = callRingResult.getResultType();
//						if (resultType == 1) {
//							Logger.i("callRing", "=========" + callRingResult.getResultMsg());
//							mCallRingView.loadData(callRingResult.getResultMsg());
//						} else {
//							mCallRingView.loadData("呼叫失败,请稍后重试!");
//						}
//					}
//
//				});

	}


}
